package com.guan.system.controller.sysuser;

import cn.hutool.core.util.StrUtil;
import com.guan.common.core.baseController.BaseController;
import com.guan.common.core.constants.HttpConstants;
import com.guan.common.core.domain.R;
import com.guan.common.core.domain.VO.LoginUserVO;
import com.guan.system.domain.sysuser.dto.LoginDTO;
import com.guan.system.domain.sysuser.dto.SysUserSavaDTO;
import com.guan.system.domain.sysuser.vo.SysUserVO;
import com.guan.system.service.sysuser.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/sysUser")
@Tag(name = "管理员用户接口")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    // 登录成功还是失败 如果失败-失败的原因 - 抽象出统一数据响应格式
    // 引入Swagger生成接口文档 url: host:port/swagger-ui/index.html
    @PostMapping("/login")
    @Operation(summary = "管理员用户登录", description = "提供帐号密码，登录管理员用户")
    public R<String> login(@RequestBody LoginDTO loginDTO) {
        R<String> loginResult = sysUserService.sysUserLogin(loginDTO.getUserAccount(), loginDTO.getPassword());
        return loginResult;
    }

    @PostMapping("/add")
    @Operation(summary = "新增管理员用户", description = "提供信息，新增管理员用户")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    public R<Void> add(@RequestBody SysUserSavaDTO sysUserSavaDTO) {
        return toR(sysUserService.add(sysUserSavaDTO));
    }

    @DeleteMapping("/{userId}")
    @Operation(summary = "删除用户", description = "通过⽤⼾id删除⽤⼾")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.PATH, description = "用户ID")
    })
    @ApiResponse(responseCode = "1000", description = "成功删除⽤⼾")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾不存在")
    public R<Void> delete(@PathVariable Long userId) {
        return null;
    }

    @Operation(summary = "⽤⼾详情", description = "根据查询条件查询⽤⼾详情")
    @GetMapping("/detail")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.QUERY, description =
                    "⽤⼾ID"),
            @Parameter(name = "sex", in = ParameterIn.QUERY, description = "⽤⼾性别")
    })
    @ApiResponse(responseCode = "1000", description = "成功获取⽤⼾信息")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾不存在")
    public R<SysUserVO> detail(Long userId, @RequestParam(required = false) String sex) {
        return null;
    }

    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return sysUserService.info(token);
    }

    @DeleteMapping("/logout")
    public R<Void> logout(HttpServletRequest request){
        String token = request.getHeader(HttpConstants.AUTHENTICATION);
        if(StrUtil.isNotEmpty(token) && token.startsWith(HttpConstants.PREFIX)){
            token = token.replaceFirst(HttpConstants.PREFIX, "");
        }
        System.out.println(token);

        return sysUserService.logout(token);
    }
}
